//地址解析的好兄弟 https://www.npmjs.com/package/path-to-regexp
var pathToRegexp = require('path-to-regexp');

/**
 * _Router 构造函数
 * @param {Array} menu 菜单
 * @param {String} path 菜单路径
 * @param {Array} children 子菜单
 */
function _Router(parentPagename, menu, children, pathInfo, others) {
  //基本信息
  this.name = menu.name;
  this.component = menu.component;
  this.pagename = menu.pagename;

  //子路由（菜单），其他路由
  this.children = children;
  this.others = others;
  //全部路由 方便使用
  this.allchildren = (children || []).concat(others || [])

  //路径信息
  this._path = pathInfo;
  this.parentPagename = parentPagename;

  //存储路由上，上次显示的hash，处理子路由
  this.curpath = '';
}
var _proto = _Router.prototype;
/**
 * 根据路径，设置路由下的活动状态，非更新，上一次的还是在
 * @param path hash路径
 */
_proto.setActive = function (path) {
  this.curpath = path;
  this.active = this.allchildren.find(item => {
    var res;
    if (res = item._path.matchRegExp.exec(path)) {
      item.setActive(path);
    }
    return res;
  })
}
/**
 * 根据hash解析出页面信息
 * @returns {Object}  页面信息
 */
_proto.getPageInfo = function (hash) {
  var _this = this;
  //没有hash，根据当前的hash 拿页面信息
  hash || (hash = _this.hash);

  var page = {
    pagename: _this.pagename,
    name: _this.name,
    component: _this.component,
    path: _this._path.path,
    //用来处理 存在子路由激活态的情况
    curpath: _this.curpath
  };

  //子页面信息
  if (_this.children) {
    var active;
    page.children = _this.children.map(item => {
      var cpage = item.getPageInfo(hash);
      if (item._path.matchRegExp.exec(hash)) {
        //找到激活也
        active = cpage;
      }
      return cpage;
    });
    //激活页信息
    page.active = active;

    if(_this.parentPagename){
      var res = _this._path.matchRegExp.exec(hash);
      if (res) {
        //解析参数
        var keys = _this._path.keys;
        if (keys && keys.length) {
          var params = {};

          keys.forEach((item, index) => {
            params[item.name] = res[index + 1]
          });
          page.params = params;
          //更新path
          page.path = _this._path.toPath(params)
        }
      }
    }
  }

  return page
}
/**
 * 清理掉历史信息
 */
_proto.clearHash = function () {
  delete this.curpath
  this.allchildren.forEach(item => item.clearHash())
}
/**
 * 生成路由
 * @param {Array} menus 菜单
 * @param {String} path 父级路径
 */
var genRouter = function (parentPagename, menus, path) {
  if (!menus) {
    return;
  }

  return menus.map(menu => {
    var keys = [];
    //拼接路径
    var npath = menu.path;
    if (!npath) {
      npath = `${path||''}/${menu.pagename}`;
    } else {
      npath = `${path||''}${npath}`;
    }
    var children = menu.children;
    return new _Router(parentPagename, menu, genRouter(menu.pagename, children, npath), {
      path: npath,
      matchRegExp: pathToRegexp(npath, keys, {
        end: !children
      }),
      keys,
      toPath: pathToRegexp.compile(npath)
    }, genRouter(menu.pagename, menu.others))
  });
}
/**
 * 根据菜单返回一个Router 集合
 * @param {Array} menus 菜单
 * @param {String} path 上一级路径
 */
var busFactory = function (menus) {

  return new _Router('', menus, genRouter('', menus.children), {}, genRouter('', menus.others))

}
export {
  busFactory
}
